package com.daocao.jy.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.daocao.jy.R;
import com.daocao.jy.activity.MyOrderActivity;
import com.daocao.jy.adapter.MyOrderAdapter;
import com.daocao.jy.api.Api;
import com.daocao.jy.api.bean.MyOrderListBean;
import com.daocao.jy.util.Network;
import com.daocao.jy.util.RxLifeUtil;
import com.daocao.jy.util.ToastUtil;
import com.daocao.jy.view.OnLoadeMoreListener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 18-4-10.
 */

public class MyOrderFragment extends BaseFragment {
    @BindView(R.id.list)
    RecyclerView list;
    Unbinder unbinder;
    @BindView(R.id.layout_empty)
    LinearLayout layoutEmpty;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;

    private int id;
    private static final String STATUS_ALL = "0";
    private static final String STATUS_UNPAY = "1";
    private static final String STATUS_PAYED = "2";
    private String selectStatus;
    //查询状态（该状态不是订单真实状态），不填或填空或填0：全部订单，1：为未付款，2：为已付款、分期中、已完成
    private MyOrderAdapter adapter;
    private OnLoadeMoreListener onLoadeMoreListener;

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_my_order;
    }

    //    "全部", "待付款", "已付款"
    @Override
    protected void initView() {
        if (getArguments() != null) {
            id = getArguments().getInt("id", 0);
        }

        onLoadeMoreListener = new OnLoadeMoreListener() {
            @Override
            public void onLoadMore(int page) {
                getOrderList(page, selectStatus);
            }
        };
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getOrderList(1, selectStatus);
            }
        });
        list.addOnScrollListener(onLoadeMoreListener);
        adapter = new MyOrderAdapter(getContext());
        adapter.setOnChooseListener(new MyOrderAdapter.OnChooseListener() {
            @Override
            public void onClick(String money, String order_no) {
                ((MyOrderActivity) getActivity()).rePay(Double.valueOf(money), order_no);
            }
        });
        list.setAdapter(adapter);

        switch (id) {
            case 0:
                selectStatus = STATUS_ALL;
                break;
            case 1:
                selectStatus = STATUS_UNPAY;
                break;
            case 2:
                selectStatus = STATUS_PAYED;
                break;
        }
        getOrderList(1, selectStatus);
    }

    public void getOrderList(int page, String status) {
        //    "全部", "待付款", "已付款"
        //状态（0：过期，1:有效，2：已使用)
        if (!Network.checkNetwork(getContext())) {
            ToastUtil.show(getContext(), "网络连接失败");
            return;
        }
        if(refresh!=null){
            refresh.setRefreshing(true);
        }
        MyOrderListBean bean = new MyOrderListBean();
        bean.status = status;
        bean.page = page + "";
        Api.api_service.getOrderList(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    adapter.add(result.datas, page == 1);
                    if (adapter.getItemCount() == 0) {
                        layoutEmpty.setVisibility(View.VISIBLE);
                    } else {
                        layoutEmpty.setVisibility(View.GONE);
                    }

                });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
